<?php

namespace app\admin\model\disassemble;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'back_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'genderdata_text',
        'is_sanyuan_text',
        'is_diving_text',
        'gettime_text',
        'confirmtime_text',
        'begintime_text',
        'endtime_text',
        'overtime_text',
        'check_images_text',
        'attachfile_text',
    ];
//    public function getAttachfileTextAttr($value, $data)
//    {
//        return (isset($data['attachfile']) && $data['attachfile']) ? request()->domain().$data['attachfile'] : '';
//    }
    public function getAttachfileTextAttr($value, $data)
    {
        $temp = [];
        if(isset($data['attachfile']) && !empty($data['attachfile']))
        {
            $temp = explode(',',$data['attachfile']);
            foreach ($temp as $key => &$value) {
                $value = request()->domain().$value;
            }
        }
        return  $temp;
    }
    public function getCheckImagesTextAttr($value, $data)
    {
        $temp = [];
        if(isset($data['check_images']) && !empty($data['check_images']))
        {
            $temp = explode(',',$data['check_images']);
            foreach ($temp as $key => &$value) {
                $value = request()->domain().$value;
            }
        }
        return  $temp;
    }
    
    public function getStatusList()
    {
        return ['0' => __('待确认'), '1' => __('待验车'), '2' => __('验车中'), '3' => __('验车待确认'), '4' => __('已完成')];
    }
    public function getIsDivingList()
    {
        return ['0' => __('自送'), '1' => __('代驾'), '2' => __('拖车'), '3' => __('自取')];
    }

    public function getGenderdataList()
    {
        return ['male' => __('男'), 'female' => __('女')];
    }

    public function getIsSanyuanList()
    {
        return ['0' => __('Is_sanyuan 0'), '1' => __('Is_sanyuan 1')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getGenderdataTextAttr($value, $data)
    {
        $value = $value ?: ($data['genderdata'] ?? '');
        $list = $this->getGenderdataList();
        return $list[$value] ?? '';
    }


    public function getIsSanyuanTextAttr($value, $data)
    {
        $value = $value ?: ($data['is_sanyuan'] ?? '');
        $list = $this->getIsSanyuanList();
        return $list[$value] ?? '';
    }


    public function getIsDivingTextAttr($value, $data)
    {
        $value = $value ?: ($data['is_diving'] ?? '');
        $list = $this->getIsDivingList();
        return $list[$value] ?? '';
    }


    public function getGettimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['gettime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getConfirmtimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['confirmtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getBegintimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['begintime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['endtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOvertimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['overtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setGettimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setConfirmtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setBegintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setOvertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPriceJsonAttr($value)
    {
        return $value === '' ? '' : ($value && is_array($value) ? json_encode($value) : $value);
    }
    protected function getPriceJsonAttr($value)
    {
        return $value === '' ? '' : ($value ? json_decode($value,true) : $value);
    }

    protected function setEndJsonAttr($value)
    {
        return $value === '' ? '' : ($value && is_array($value) ? json_encode($value) : $value);
    }
//    protected function getEndJsonAttr($value)
//    {
//        return $value === '' ? '' : ($value ? json_decode($value,true) : $value);
//    }
    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'userid', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function disassemble()
    {
        return $this->belongsTo('app\admin\model\Disassemble', 'end_storeid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
